package com.ellecity06.common.widgets.decoration

import android.annotation.SuppressLint
import android.graphics.Canvas
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.view.View
import androidx.annotation.ColorRes
import androidx.annotation.DimenRes
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.ellecity06.common.R
import com.ellecity06.common.utils.appContext

/**
 * @author zavier
 * @time 2022/3/30 17:15
 * @des  通用LinearLayoutManager分割线
 */
@SuppressLint("UseCompatLoadingForDrawables")
class NormalLinearDecoration constructor(
    @DimenRes size: Int = R.dimen.dp_1,
    @ColorRes color: Int = R.color.transparent,
    var isLastItemAddDecoration: Boolean = true
) : RecyclerView.ItemDecoration() {

    // 分割线的高度或宽度
    private var dividerSize = -1

    // 分割线颜色
    private var divider: Drawable

    init {
        dividerSize = appContext.resources.getDimensionPixelSize(size)
        divider = appContext.resources.getDrawable(
            color,
            appContext.theme
        )
    }

    override fun onDraw(canvas: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        val layoutManager = parent.layoutManager
        if (layoutManager is LinearLayoutManager) {
            val orientation = layoutManager.orientation
            if (orientation == LinearLayoutManager.VERTICAL) {
                drawHorizontalLine(canvas, parent)
            } else {
                drawVerticalLine(canvas, parent)
            }
        }
    }

    /**
     * 画横线
     */
    private fun drawHorizontalLine(canvas: Canvas, parent: RecyclerView) {
        val left = parent.paddingLeft
        val right = parent.width - parent.paddingRight
        val childCount = parent.childCount
        for (i in 0 until childCount) {
            val child = parent.getChildAt(i)
            val params = child.layoutParams as RecyclerView.LayoutParams
            val top = child.bottom + params.bottomMargin
            val bottom = top + dividerSize
            divider.setBounds(left, top, right, bottom)
            divider.draw(canvas)
        }
    }

    /**
     * 画竖线
     */
    private fun drawVerticalLine(canvas: Canvas, parent: RecyclerView) {
        val top = parent.paddingTop
        val bottom = parent.height - parent.paddingBottom
        val childCount = parent.childCount
        for (i in 0 until childCount) {
            val child = parent.getChildAt(i)
            val params = child.layoutParams as RecyclerView.LayoutParams
            val left = child.right + params.rightMargin
            val right = left + dividerSize
            divider.setBounds(left, top, right, bottom)
            divider.draw(canvas)
        }
    }

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        val itemCount = parent.adapter?.itemCount
        var itemPosition = parent.getChildAdapterPosition(view)
        itemPosition += 1

        // 最后一个item是否需要分割线
        if (!isLastItemAddDecoration) {
            if (itemPosition == itemCount) {
                return
            }
        }

        val layoutManager = parent.layoutManager
        if (layoutManager is LinearLayoutManager) {
            val orientation = layoutManager.orientation
            if (orientation == LinearLayoutManager.VERTICAL) {
                outRect.set(0, 0, 0, dividerSize)
            } else {
                outRect.set(0, 0, dividerSize, 0)
            }
        }
    }
}